import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;

import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

public class HbaseClient {
    Configuration conf = null;
    Connection conn = null;

    @Before
    public void init() throws IOException {
        //获取一个配置文件对象
        conf = HBaseConfiguration.create();

        conf.set("hbase.zookeeper.quorum", "192.168.95.121,192.168.95.122");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        //通过conf获取到hbase集群的连接
        conn = ConnectionFactory.createConnection(conf);
        System.out.println("11111");
    }

    //创建一张hbase表
    @Test
    public void createTable() throws IOException {
        //获取HbaseAdmin对象用来创建表
        HBaseAdmin admin = (HBaseAdmin) conn.getAdmin();
        //创建Htabledesc描述器，表描述器
        final HTableDescriptor worker = new HTableDescriptor(TableName.valueOf("userRelation"));
        //指定列族
        worker.addFamily(new HColumnDescriptor("friends"));
        admin.createTable(worker);
        System.out.println("worker表创建成功！！");
    }

    //插入一条数据
    @Test
    public void putData() throws IOException {
        //需要获取一个table对象
        final Table worker = conn.getTable(TableName.valueOf("userRelation"));

        //准备put对象
        final Put put = new Put(Bytes.toBytes("uid1"));//指定rowkey

        put.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid2"), Bytes.toBytes("uid2"));
        //插入数据，参数类型是put
        worker.put(put);
        //准备list<puts>,可以执行批量插入
        //关闭table对象
        worker.close();
        System.out.println("插入数据到worker表成功！！");
    }

    //查询数据
    @Test
    public void getData() throws IOException {
        //准备table对象
        final Table worker = conn.getTable(TableName.valueOf("userRelation"));
        //准备get对象
        final Get get = new Get(Bytes.toBytes("uid1"));
        //指定查询某个列族或者列
        get.addFamily(Bytes.toBytes("friends"));
        //执行查询
        final Result result = worker.get(get);
        //获取到result中所有cell对象
        final Cell[] cells = result.rawCells();
        //遍历打印
        for (Cell cell : cells) {
            final String rowkey = Bytes.toString(CellUtil.cloneRow(cell));
            final String f = Bytes.toString(CellUtil.cloneFamily(cell));
            final String column = Bytes.toString(CellUtil.cloneQualifier(cell));
            final String value = Bytes.toString(CellUtil.cloneValue(cell));

            System.out.println("rowkey-->" + rowkey + "--;cf-->" + f + "---;column--->" + column + "--;value-->" + value);
        }
        worker.close();
    }


    //删除一条数据
    @Test
    public void deleteData() throws IOException {
        //需要获取一个table对象
        final Table worker = conn.getTable(TableName.valueOf("userRelation"));

        //准备delete对象
        final Delete delete = new Delete(Bytes.toBytes("uid2"));
        //执行删除
        worker.delete(delete);
        //关闭table对象
        worker.close();
        System.out.println("删除数据成功！！");
    }

    @Test
    public void deleteFriedns() throws IOException {
/*
1 需要根据传入用户删除其列族中对应好友的列
2 其好友的列族对应需要删除该用户的列
*/
        final Table relation = conn.getTable(TableName.valueOf("userRelation"));
        final Delete delete = new Delete(Bytes.toBytes("uid4"));
        delete.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid5"));
        relation.delete(delete);
    }


    //释放连接
    @After
    public void realse() {
        if (conn != null) {
            try {
                conn.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
